//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.pentaho.kettle.plugin.mqtt.ui.steps.producer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.util.StringUtil;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDialogInterface;
import org.pentaho.di.ui.trans.step.BaseStepXulDialog;
import org.pentaho.kettle.plugin.mqtt.steps.producer.ProducerStepMeta;
import org.pentaho.ui.xul.binding.BindingConvertor;
import org.pentaho.ui.xul.binding.Binding.Type;
import org.pentaho.ui.xul.components.XulMenuList;
import org.pentaho.ui.xul.components.XulTextbox;

public class ProducerStepDialog extends BaseStepXulDialog implements StepDialogInterface {
    private static final Class<?> PKG = ProducerStepMeta.class;
    private String workingStepname;
    private XulMenuList<?> messageFieldsNames;
    private List<ValueMetaInterface> messageFields = new ArrayList();
    private ProducerStepMetaMapper metaMapper;

    public ProducerStepDialog(Shell parent, Object in, TransMeta tr, String sname) throws Throwable {
        super("org/pentaho/kettle/plugin/mqtt/ui/producer/dialog.xul", parent, (BaseStepMeta)in, tr, sname);
        this.init();
    }

    public void init() throws Throwable {
        this.workingStepname = this.stepname;
        this.metaMapper = new ProducerStepMetaMapper();
        this.metaMapper.loadMeta((ProducerStepMeta)this.baseStepMeta);
        RowMetaInterface inputRow = null;

        try {
            inputRow = this.transMeta.getPrevStepFields(this.stepMeta);
        } catch (KettleStepException var4) {
        }

        if (!StringUtil.isEmpty(this.metaMapper.getpMessage())) {
            this.messageFields.add(new ValueMeta(this.metaMapper.getpMessage()));
        }

        if (inputRow != null) {
            Iterator var2 = inputRow.getValueMetaList().iterator();
            while(true) {
                ValueMetaInterface field;
                do {
                    if (!var2.hasNext()) {
                        break;
                    }

                    field = (ValueMetaInterface)var2.next();
                } while(!StringUtil.isEmpty(this.metaMapper.getpMessage()) && this.metaMapper.getpMessage().equals(field.getName()));

                this.messageFields.add(new ValueMeta(field.getName()));
            }
        }

        this.setTextBoxValue("pBroker-field", this.metaMapper.getpBroker());
        this.setTextBoxValue("pTopic-field", this.metaMapper.getpTopic());
        this.setTextBoxValue("pClientID-field", this.metaMapper.getpClientId());
        this.setTextBoxValue("qos-field", Integer.toString(this.metaMapper.getpQOS()));
        this.bf.setBindingType(Type.ONE_WAY);
        this.bf.createBinding("step-name", "value", this, "stepName", new BindingConvertor[0]);
        this.bf.createBinding(this, "stepName", "step-name", "value", new BindingConvertor[0]).fireSourceChanged();
        this.bf.createBinding(this, "messageFields", "message-fieldname", "elements", new BindingConvertor[0]).fireSourceChanged();
        this.messageFieldsNames = (XulMenuList)this.getXulDomContainer().getDocumentRoot().getElementById("message-fieldname");
        if (this.messageFieldsNames != null && this.messageFieldsNames.getElements().size() > 0) {
            this.messageFieldsNames.setSelectedIndex(0);
        }

    }

    private void setTextBoxValue(String textbox, String value) {
        ((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById(textbox)).setValue(value);
    }

    protected Class<?> getClassForMessages() {
        return ProducerStepMeta.class;
    }

    public void onAccept() {
        this.metaMapper.setpBroker(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("pBroker-field")));
        this.metaMapper.setpMessage(this.messageFieldsNames.getValue());
        this.metaMapper.setpTopic(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("pTopic-field")));
        this.metaMapper.setpClientId(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("pClientID-field")));
        this.metaMapper.setpQOS(Integer.parseInt(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("qos-field"))));
        if (!this.workingStepname.equals(this.stepname)) {
            this.stepname = this.workingStepname;
            this.baseStepMeta.setChanged();
        }

        this.metaMapper.saveMeta((ProducerStepMeta)this.baseStepMeta);
        this.dispose();
    }

    private String fetchValue(XulTextbox textbox) {
        String result = "";
        if (textbox != null && !StringUtil.isEmpty(textbox.getValue())) {
            try {
                result = textbox.getValue();
            } catch (NumberFormatException var4) {
                this.log.logError(BaseMessages.getString("HadoopEnter.Error.ParseInteger", textbox.getValue()));
            }
        }

        return result;
    }

    public void onCancel() {
        this.setStepName((String)null);
        this.dispose();
    }

    public void setStepName(String stepname) {
        this.workingStepname = stepname;
    }

    public String getStepName() {
        return this.workingStepname;
    }

    public List<ValueMetaInterface> getMessageFields() {
        return this.messageFields;
    }
}
